﻿package com.yy.yyui.slider {
    import flash.geom.*;
    import flash.events.*;
    import flash.display.*;
    import com.yy.yycompnent.slide.SliderBar;

    public class EasySlider extends EventDispatcher {

        private const LIST:Array = ["scrollDown", "scrollUp", "bar"];

        private var _min:Number = 0;
        private var _isDrag:Boolean = false;
        private var _max:Number = 0;
        private var _scrollUp:SimpleButton = null;
        private var _scrollDown:SimpleButton = null;
        private var _scrollTrack:MovieClip = null;
        private var _pageCount:int = 0;
        private var _currentBtn:SimpleButton = null;
        private var _sliderView:MovieClip = null;
        private var _speed:Number = 1;
        private var _bar:MovieClip = null;
        private var _maxCount:int = 0;
        private var _value:Number = 0;
        private var _sliderBar:SliderBar = null;
        private var _maxBarHeight:Number = 0;

        public function EasySlider(_arg1:MovieClip){
            _sliderView = null;
            _scrollUp = null;
            _scrollDown = null;
            _scrollTrack = null;
            _sliderBar = null;
            _bar = null;
            _pageCount = 0;
            _maxCount = 0;
            _maxBarHeight = 0;
            _speed = 1;
            _value = 0;
            _min = 0;
            _max = 0;
            _currentBtn = null;
            _isDrag = false;
            super();
            _sliderView = _arg1;
            _scrollUp = _sliderView.upBtn;
            _scrollDown = _sliderView.downBtn;
            _scrollTrack = _sliderView.track;
            _bar = _sliderView.sliderBar;
            _sliderBar = new SliderBar(_bar);
            _sliderBar.hideBar();
        }
        public function set pageCount(_arg1:int):void{
            _pageCount = _arg1;
        }
        private function dragHandler(_arg1:MouseEvent):void{
            _sliderBar.select(true);
            _bar.startDrag(false, new Rectangle(0, _min, 0, (_max - _min)));
            _isDrag = true;
            _bar.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
            _bar.addEventListener(Event.ENTER_FRAME, scrollHandler);
        }
        public function get y():Number{
            return (_sliderView.y);
        }
        public function set speed(_arg1:Number):void{
            _speed = _arg1;
        }
        private function stopDragHandler(_arg1:MouseEvent):void{
            _sliderBar.select(false);
            _bar.stopDrag();
            _isDrag = false;
            _arg1.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
            _bar.removeEventListener(Event.ENTER_FRAME, scrollHandler);
        }
        public function set maxCount(_arg1:int):void{
            _maxCount = _arg1;
        }
        public function set y(_arg1:Number):void{
            _sliderView.y = _arg1;
        }
        private function upHandler(_arg1:MouseEvent):void{
            _bar.removeEventListener(Event.ENTER_FRAME, scrollHandler);
            _arg1.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
        }
        public function setScrollRange(_arg1:Number=0):void{
            _scrollUp.y = _scrollTrack.y;
            _scrollDown.y = (_scrollTrack.y + _scrollTrack.height);
            _min = ((_scrollTrack.y + (_scrollUp.height / 2)) + _arg1);
            _max = ((((_scrollTrack.y + _scrollTrack.height) - (_scrollDown.height / 2)) - _sliderBar.height) - _arg1);
            _min = Math.round(_min);
            _max = Math.round(_max);
            _sliderBar.y = _min;
            _speed = (((_max - _min) / (_maxCount - _pageCount)) / 4);
        }
        private function scrollHandler(_arg1:Event):void{
            if (_isDrag){
                _sliderBar.y = _bar.y;
            } else {
                _sliderBar.y = (_sliderBar.y + _speed);
            };
            adjustPosition();
            if ((_max - _min) != 0){
                _value = ((100 * (_sliderBar.y - _min)) / (_max - _min));
                dispatchEvent(new Event(Event.CHANGE));
            };
        }
        private function addMouseEffect(_arg1:Boolean):void{
            var _local2:int;
            _local2 = 0;
            while (_local2 < LIST.length) {
                _local2++;
            };
        }
        public function get width():Number{
            return (_sliderView.width);
        }
        public function setPlace(_arg1:Number):void{
            _value = _arg1;
            _sliderBar.y = (((_value * (_max - _min)) / 100) + _min);
        }
        private function downHandler(_arg1:MouseEvent):void{
            if (_arg1.currentTarget == _scrollUp){
                _speed = -(Math.abs(_speed));
                _currentBtn = _scrollUp;
            } else {
                _speed = Math.abs(_speed);
                _currentBtn = _scrollDown;
            };
            _bar.addEventListener(Event.ENTER_FRAME, scrollHandler);
            _currentBtn.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
        }
        public function setBarHeight(_arg1:Number, _arg2:Number, _arg3:Number=-1):void{
            _pageCount = _arg1;
            _maxCount = _arg2;
            _scrollTrack.height = ((_arg3)==-1) ? _scrollTrack.height : ((_arg3 - (_scrollDown.height / 2)) - (_scrollUp.height / 2));
            _maxBarHeight = ((_scrollTrack.height - (_scrollDown.height / 2)) - (_scrollUp.height / 2));
            _arg3 = ((_maxBarHeight * _pageCount) / _maxCount);
            if ((((_arg3 >= _maxBarHeight)) || ((_maxCount <= _pageCount)))){
                _sliderBar.hideBar();
                removeEvent();
            } else {
                _sliderBar.height = _arg3;
                addEvent();
                _sliderBar.showBar();
            };
        }
        private function adjustPosition():Boolean{
            if (_sliderBar.y < _min){
                _sliderBar.y = _min;
                return (false);
            };
            if (_sliderBar.y > _max){
                _sliderBar.y = _max;
                return (false);
            };
            return (true);
        }
        public function set value(_arg1:Number):void{
            _value = _arg1;
            _sliderBar.y = (((_value * (_max - _min)) / 100) + _min);
            dispatchEvent(new Event(Event.CHANGE));
        }
        private function wheelHandler(_arg1:MouseEvent):void{
            if (_arg1.delta != 0){
                _sliderBar.y = (_sliderBar.y - ((Math.abs(_speed) * Math.abs(_arg1.delta)) / _arg1.delta));
                adjustPosition();
                if ((_max - _min) != 0){
                    _value = ((100 * (_sliderBar.y - _min)) / (_max - _min));
                    dispatchEvent(new Event(Event.CHANGE));
                };
            };
        }
        public function set x(_arg1:Number):void{
            _sliderView.x = _arg1;
        }
        private function removeEvent():void{
            _scrollUp.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
            _scrollDown.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
            _bar.removeEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
            _bar.removeEventListener(Event.ENTER_FRAME, scrollHandler);
            _sliderView.parent.removeEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
            _scrollTrack.removeEventListener(MouseEvent.MOUSE_DOWN, trackDownHandler);
            addMouseEffect(false);
        }
        public function get value():Number{
            return (_value);
        }
        private function addEvent():void{
            _scrollUp.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
            _scrollDown.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
            _bar.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
            _sliderView.parent.addEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
            _scrollTrack.addEventListener(MouseEvent.MOUSE_DOWN, trackDownHandler);
            addMouseEffect(true);
        }
        private function trackDownHandler(_arg1:MouseEvent):void{
            if (_arg1.localY > _sliderBar.y){
                _speed = Math.abs(_speed);
            } else {
                _speed = -(Math.abs(_speed));
            };
            _bar.addEventListener(Event.ENTER_FRAME, scrollHandler);
            _bar.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
        }
        public function get x():Number{
            return (_sliderView.x);
        }
    }
}